package app.modules.security.application.web;

import app.modules.common.dto.web.WebIdDTO;
import app.modules.common.util.web.PageUtil;
import app.modules.security.business.service.resourcee.ResourceService;
import app.modules.security.dto.web.resource.WebResourceDTO;
import app.modules.security.dto.web.resource.WebResourceQueryDTO;
import app.modules.security.dto.web.resource.WebResourceSaveDTO;
import app.modules.security.dto.web.resource.WebResourceUpdateDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jasmine.framework.web.entity.WebResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author mh.z
 */
@Tag(name = "接口管理/资源API")
@RestController
public class ResourceController {
    private final ResourceService resourceService;

    public ResourceController(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    @Operation(summary = "分页查询资源")
    @PostMapping("/api/security/resource/pageResourcesByCond")
    public ResponseEntity<WebResult<List<WebResourceDTO>>> pageWebPermissionDTOsByCond(
            @RequestBody WebResourceQueryDTO queryDTO) {
        Page page = PageUtil.getPage(queryDTO);

        List<WebResourceDTO> recordList = resourceService.pageWebResourceDTOsByCond(queryDTO, page);
        WebResult result = PageUtil.getResult(recordList, page);

        return result.toOkEntity();
    }

    @Operation(summary = "查询指定 ID 的资源")
    @PostMapping("/api/security/resource/getResourceById")
    public ResponseEntity<WebResult<WebResourceDTO>> getResourceById(
            @RequestBody WebIdDTO dto) {
        Long id = dto.getId();
        WebResourceDTO resourceDTO = resourceService.getWebResourceDtoById(id);

        return WebResult.success(resourceDTO).toOkEntity();
    }

    @Operation(summary = "保存资源")
    @PostMapping("/api/security/resource/saveResource")
    public ResponseEntity<WebResult<WebResourceDTO>> saveResource(
            @Valid @RequestBody WebResourceSaveDTO saveDTO) {
        WebResourceDTO resourceDTO = resourceService.saveResource(saveDTO);
        WebResult result = WebResult.success(resourceDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "更新资源")
    @PostMapping("/api/security/resource/updateResource")
    public ResponseEntity<WebResult<WebResourceDTO>> updateResource(
            @Valid @RequestBody WebResourceUpdateDTO updateDTO) {
        WebResourceDTO resourceDTO = resourceService.updateResource(updateDTO);
        WebResult result = WebResult.success(resourceDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "删除指定 ID 的资源")
    @PostMapping("/api/security/resource/deleteResourceById")
    public ResponseEntity<WebResult<Object>> deleteResourceById(
            @RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        resourceService.deleteRecordById(recordId);

        return WebResult.success().toOkEntity();
    }

}
